Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add deref suggestion for type mismatch binary op #112961

Closed
wants to merge 1 commit into from

Conversation

bvanjoi
Copy link
Contributor

@bvanjoi bvanjoi commented Jun 23, 2023

Fixes #112958

Find the mutable expression in binary operation and add * suggestion before it

@rustbot
Copy link
Collaborator

rustbot commented Jun 23, 2023

r? @petrochenkov

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 23, 2023
@bvanjoi bvanjoi changed the title fix: add deref suggestion for type mismatch compare binary op fix: add deref suggestion for type mismatch binary op Jun 23, 2023
})) = self.tcx.hir().find_parent(expr.hir_id) && mutability.is_mut() {
let deref_expr = if expr.hir_id == left.hir_id { right } else { left };
let sugg = vec![(deref_expr.span.shrink_to_lo(), "*".to_string())];
return Some((
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why I didn't think it needed to be wrapped in parentheses is that I thought it would throw an error beforehand.

let sugg = vec![(deref_expr.span.shrink_to_lo(), "*".to_string())];
return Some((
sugg,
format!("consider dereference here"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
format!("consider dereference here"),
format!("consider dereferencing here"),

Further, I think it'd make sense to use a translatable diagnostic here, so less needs to be migrated later on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd make sense to use a translatable diagnostic here

The return type of suggest_deref_or_ref is a tuple instead of Diagnostic, so I think it would be better to change it in another PR

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

if let Some(hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Binary(_, left, right),
..
})) = self.tcx.hir().find_parent(expr.hir_id) && mutability.is_mut() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this needs special-casing for mutable references.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean mutability.is_mut()? It does to prevent this problem:

let a = String::new();
let b = String::new();
let _ = a + b // should suggestion a + &b, rather than *a + b

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well what we really should be doing is checking whether the obligation Lhs: BinOp<Rhs> holds after dereffing the left-hand side. Not really sure if that's possible here, so I guess maybe give that a try or else I can approve..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking whether the obligation Lhs: BinOp holds after dereffing the left-hand side

I haven't found a definitive solution yet. Do you have any thoughts or suggestions?

@petrochenkov
Copy link
Contributor

r? @compiler-errors

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment

if let Some(hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Binary(_, left, right),
..
})) = self.tcx.hir().find_parent(expr.hir_id) && mutability.is_mut() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well what we really should be doing is checking whether the obligation Lhs: BinOp<Rhs> holds after dereffing the left-hand side. Not really sure if that's possible here, so I guess maybe give that a try or else I can approve..

@compiler-errors
Copy link
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 10, 2023
@Dylan-DPC
Copy link
Member

@bvanjoi any updates no this?

@bvanjoi
Copy link
Contributor Author

bvanjoi commented Aug 21, 2023

checking whether the obligation Lhs: BinOp<Rhs> holds after dereffing the left-hand side.

I haven't found a suitable check for this yet. We can wait for suggestions from @compiler-errors.

@bors
Copy link
Contributor

bors commented Nov 1, 2023

☔ The latest upstream changes (presumably #117482) made this pull request unmergeable. Please resolve the merge conflicts.

@JohnCSimon
Copy link
Member

@bvanjoi
ping from triage - can you post your status on this PR? This PR has not received an update in a few months. Thank you!

FYI: when a PR is ready for review, send a message containing
@rustbot ready to switch to S-waiting-on-review so the PR is in the reviewer's backlog.

Or if you're not going to continue, please close it. Thank you!

@bvanjoi bvanjoi closed this Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

hint to consider dereferencing instead of mutably borrowing an immutable variable
9 participants